﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    /// <summary>
    /// 元数据依赖关系持久化类
    /// </summary>
    public class MetaDataDependencyVersionDao : DaoService<MetaDataDependencyVersion>
    {
        public MetaDataDependencyVersionDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_DependencyVersion";
            }
        }

        public override List<string> ExceptColumnName
        {
            get
            {
                return new List<string>()
                {
                    ""
                };
            }
        }

        public override Dictionary<string, object> GetDataMapping(MetaDataDependencyVersion data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("DependencyType");
            datas.Add("DependencyType", (int)(data.DependencyType));            
            return datas;
        }

        public override MetaDataDependencyVersion ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("DependencyType");
            var data = base.ConvertToObject(myCol, row);

            if (row["DependencyType"] != null)
                data.DependencyType = (MetaDataDependencyType)Enum.Parse(typeof(MetaDataDependencyType),
                                                Convert.ToString(row["DependencyType"]));

            return data;
        }

        public List<MetaDataDependencyVersion> GetDependencyVersions(string projectBranchId, string metaDataId, string metaDataVersionID) {
            var dependMetadatas = new List<MetaDataDependencyVersion>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "MetaDataID=@MetaDataID AND ProjectBranchID=@ProjectBranchID"; 
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            if (string.IsNullOrEmpty(metaDataVersionID) == false) {
                sqlCondition = string.Format("{0} AND MetadataVersionID=@MetadataVersionID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetadataVersionID", TDbDataType.VarChar, metaDataVersionID));
            }
            var ds = DB.ExecuteDataSet(string.Format(@"select ID, MetaDataID, MetadataVersionID, MetadataVersion, ParentMetaDataID, ParentMetadataVersionID, ParentMetadataVersion, ProjectBranchID, DependencyType, Creator, CreateTime, LastModifier, LastModifyTime FROM Meta_DependencyVersion WHERE {0}", sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {                    
                    var dep = this.ConvertToObject(this.GetColumns(), row);                    
                    dependMetadatas.Add(dep);
                }
            }
            return dependMetadatas;
        }

       

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="IsMetaDataDelete"></param>
        public void DeleteMetaDataDependency(string metadataId, string projectBranchId, bool IsMetaDataDelete, string userID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            string sqlCondition = "MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID";
            if (IsMetaDataDelete) {
                sqlCondition = "(MetaDataID=@MetaDataID Or ParentMetaDataID=@MetaDataID) and ProjectBranchID=@ProjectBranchID";
            }
            if (string.IsNullOrEmpty(userID) == false) {
                sqlCondition = string.Format("{0} AND CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userID));

            }
            this.DB.ExecSqlStatement(string.Format(@"update {0} set deleteMark = 1 WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }

        public void DeleteAndUpdateMetaDataDependency(string metadataId, string projectBranchId, string userID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userID));
            this.DB.ExecSqlStatement("Delete from Meta_Dependency WHERE MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID AND (CheckOutUserID is null or CheckOutUserID='')", paramsList.ToArray());
            this.DB.ExecSqlStatement("Update Meta_Dependency set CheckOutUserID='' where MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID AND DeleteMark=0 AND CheckOutUserID=@CheckOutUserID", paramsList.ToArray());
        }

    }
}
